<?php

if (!$l_loged) {
    include "login_error.php";
} else { if ($l_role < 0) {
        include "role_error.php";
    } else {
        ?>
<h2 class="cont_page">Přidání nového úkolu</h2>
<?PHP

function datum_ok($den, $mesic, $rok) {
    if ((int)$rok < (int)date('Y')) {
        return false;
    } else {
        if ((int)$rok > (int)date('Y')) {
            return true;
        } else {
            if ((int)$mesic > (int)date('m')) {
                return true;
            } else {
                if ((int)$mesic < (int)date('m')) {
                    return false;
                } else {
                    if ((int)$den < (int)date('d')) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }
        }
    }
}

if (isset($_GET['btn']) && (($_GET['txt'] != '') && ($_GET['priorita'] != '') && ($_GET['splnit'] != ''))) {
    $date_parts = preg_split("/[.:,-]/", $_GET['splnit']);
    if (checkdate((int)$date_parts[1], (int)$date_parts[0], (int)$date_parts[2])){
        $ctrldate = (int)$date_parts[0].".".(int)$date_parts[1].".".(int)$date_parts[2];
        if (datum_ok($date_parts[0],$date_parts[1],$date_parts[2])) {
            if (task_add($_GET['txt'],$_GET['priorita'], $ctrldate, $_GET['hotovo'])) {
                echo('<p class="ok_desc">Úkol přidán..</p><br><br>');
            } else {
                echo('<p class="err_desc">Nezdařil se zápis do databáze..</p><br><br>');
            }
        }
    }
}



?>
<form action="newtask.php" method="get" id="form_task">
    <fieldset  class="user">
        <legend>Nový úkol</legend>
        <p <?PHP if (isset($_GET['btn']) && ($_GET['txt'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="txt" class="nes">*Text:</label>
            <input maxlength="500" size="80" value="<?php echo htmlspecialchars($_GET['txt']);?>" type="text" id="txt" name="txt">
        </p>

        <p <?PHP if (isset($_GET['btn']) && ($_GET['priorita'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="priorita" class="nes">*Priorita: </label>
            <input maxlength="2" type="text" value="<?php echo htmlspecialchars($_GET['priorita']);?>" id="priorita" name="priorita"> 0-nizka, 99-nejvyšší
        </p>

        <p <?PHP if (isset($_GET['btn']) && ($_GET['splnit'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="splnit" class="nes">*Splnit do: </label>
            <input type="text" maxlength="20" value="<?php if (isset($_GET['btn'])){echo htmlspecialchars($_GET['splnit']);} else {echo date('d.m.Y', strtotime('+1 month'));}?>" id="splnit" name="splnit"> ve formátu dd.mm.yyyy
        </p>
        <p class="ok_form">
            <label for="hotovo">Hotovo: </label>
            <input type="text" maxlength="100" value="<?php echo htmlspecialchars($_GET['hotovo']);?>" id="hotovo" name="hotovo"> %
        </p>
      <?PHP
      if (isset($_GET['btn']) && (($_GET['txt'] == '') || ($_GET['priorita'] == '') || ($_GET['splnit'] == ''))) {
          echo ('<p class="err_desc">* Všechny prvky touto barvou je nutné vyplnit!</p>');

          }
      if ((isset($_GET['btn'])) && (!datum_ok($date_parts[0], $date_parts[1], $date_parts[2]))){
          echo ('<p class="err_desc">Nelze zadat datum, které již bylo!</p>');
      }
      if (isset($_GET['btn'])){
        $date_part = preg_split("/[.:,-]/", $_GET['splnit']);
        if (!checkdate((int)$date_part[1], (int)$date_part[0], (int)$date_part[2])){
            echo ('<p class="err_desc">Datum neni ve spravnem tvaru!</p>');
        }
      }
      ?>

    <p>
        <input class="btn" type="submit" value="Zadat" id="btn" name="btn">
    </p>
    </fieldset>


</form>
<script type="text/javascript">
    var form = document.getElementById('form_task');
    form.onsubmit = ctrl_usr;

    function ctrl_usr(){
        var txt = document.getElementById('txt');
        var pri = document.getElementById('priorita');
        var spl = document.getElementById('splnit');

        if (txt.value == ''){
            alert('Chybí vypln\u011bný text!');
            return(true);
        }
        if (pri.value == ''){
            alert('Chybí vypln\u011bná priorita!');
            return(true);
        }
        if (spl.value == ''){
            alert('Chybí vypln\u011bno datum splnění!');
            return(true);
        }
        return(true);
    }
</script>

<?php
}

}
?>
